********************************************************************************
**************************** Prepare and Clean Data ****************************
********************************************************************************

****************************************************
* Experimental Data and Parental Questionnaire 
****************************************************			
import delimited "0_data_raw_ascii\Jordan_Dataset_Raw.csv", encoding(UTF-8) case(preserve) clear

* Give Stata/Windows 5 Secs to process the data management
sleep 5000

* Meta Data
	label 	var	No	"ID"
	label 	var	ClassID "Class ID"

	** Morning/Afternoon shift variable (numeric)
	rename 		Isthischildinthemorningshi 	Shift
	gen 		shift = .
	replace 	shift = 0 if Shift == "Morning"
	replace 	shift = 1 if Shift == "Afternoon"

	label 	def shift 0 "Morning" 1 "Afternoon"
	label	val shift shift
	label 	var shift "Shift"

	** Treatment groups
	rename Whattreatmentgroupisthis TreatmentGroup 
	gen treatment = ""
	replace	treatment =	"1M" if Shift == "Morning"		&	TreatmentGroup  ==	1
	replace	treatment =	"1A" if Shift == "Afternoon"	&	TreatmentGroup  ==	1
	replace	treatment =	"2M" if Shift == "Morning"		&	TreatmentGroup  ==	2
	replace	treatment =	"2A" if Shift == "Afternoon"	&	TreatmentGroup  ==	2
	replace	treatment =	"3M" if Shift == "Morning"		&	TreatmentGroup  ==	3
	replace	treatment =	"3A" if Shift == "Afternoon"	&	TreatmentGroup  ==	3
	replace	treatment =	"4M" if Shift == "Morning"		&	TreatmentGroup  ==	4
	replace	treatment =	"4A" if Shift == "Afternoon"	&	TreatmentGroup  ==	4
	label 	var	TreatmentGroup "Treatment Group"
	
* Experimental Outcomes
	
	** Rename main outcomes
	rename Howmanytoysdoesthechildkee Game1_Keeping
	rename Howmanytoysdoesthechildke2 Game2_Keeping
	rename Howmanytoysdoesthechildgiv Game3_Giving_In


	** Redefining the outcome variable in terms of "giving" rather than "keeping"
		*** Game 1
		gen 			Game1_Giving = .
		replace			Game1_Giving = 5 - Game1_Keeping

		label var Game1_Giving "Amount given to other child in game 1"

		*** Game 2
		gen 			Game2_Giving = .
		replace			Game2_Giving = 5 - Game2_Keeping
		
		label var Game2_Giving "Amount given to other child in game 2"
		
		*** Game 3
		gen 			Game3_Giving_Out = .
		replace			Game3_Giving_Out = 5 - Game3_Giving_In

		label var Game3_Giving_Out "Amount given to out-group child in game 3"
	
	** Give meaning to allocations in game 1 and 2
		*** Game 1
		gen 		Game1_RecipientOutgroup = .
		replace 	Game1_RecipientOutgroup = 1 if TreatmentGroup == 3 
		replace 	Game1_RecipientOutgroup = 1 if TreatmentGroup == 4 
		replace 	Game1_RecipientOutgroup = 0 if TreatmentGroup == 1 
		replace 	Game1_RecipientOutgroup = 0 if TreatmentGroup == 2 

		label 	def Game1_RecipientOutgroup 0 "in-group" 1 "out-group"
		label	val Game1_RecipientOutgroup Game1_RecipientOutgroup
		
		label 	var	Game1_RecipientOutgroup "Recipient in game 1 is from the out-group (=1)"

		*** Game 2
		gen 		Game2_RecipientOutgroup = .
		replace 	Game2_RecipientOutgroup = 1 if TreatmentGroup == 1 
		replace 	Game2_RecipientOutgroup = 1 if TreatmentGroup == 2 
		replace 	Game2_RecipientOutgroup = 0 if TreatmentGroup == 3 
		replace 	Game2_RecipientOutgroup = 0 if TreatmentGroup == 4 

		label 	def Game2_RecipientOutgroup 0 "in-group" 1 "out-group"
		label	val Game2_RecipientOutgroup Game2_RecipientOutgroup
		
		label 	var	Game2_RecipientOutgroup "Recipient in game 2 is from the out-group (=1)"

	** Defining a giving variable to in-group and out-group for tasks 1&2
		gen 		Game12_Giving_In = .
		replace 	Game12_Giving_In = Game1_Giving if Game1_RecipientOutgroup == 0
		replace 	Game12_Giving_In = Game2_Giving if Game2_RecipientOutgroup == 0

		gen 		Game12_Giving_Out = .
		replace 	Game12_Giving_Out = Game1_Giving if Game1_RecipientOutgroup == 1
		replace 	Game12_Giving_Out = Game2_Giving if Game2_RecipientOutgroup == 1
		
		label var 	Game12_Giving_In "Amount given to in-group child in Game 1/2"
		label var 	Game12_Giving_Out "Amount given to out-group child in Game 1/2"
		
	** Create discrimination indices
	*** Within Subject Change in Giving between In and Outgroup in Games 1 and 2 (Game 1/2 Discrimination Measure)
		gen 		Game12_GivingInMinusGivingOut = .
		replace 	Game12_GivingInMinusGivingOut = Game12_Giving_In - Game12_Giving_Out
		
		label 	var	Game12_GivingInMinusGivingOut "Discrimination in Game 1/2"
		
	*** Game Three Discrimination Measure			
		gen			Game3_Giving_2_5_MinusGivingOut = .
		replace 	Game3_Giving_2_5_MinusGivingOut = 2.5 - Game3_Giving_Out	
		
		label 	var	Game3_Giving_2_5_MinusGivingOut "Discrimination in Game 3"
		
	*** Aggregate discrimination measure
		cap gen SharingGameDiscrimination = Game12_GivingInMinusGivingOut + Game3_Giving_In - 2.5
		label var SharingGameDiscrimination "Discrimination (Sharing Games)"
		
* Parental Attitudes
	** Rename Parental Attitudes
	rename 	TheSyriancrisisisatragedy 	ParAtt_Tragedy
	rename 	Jordanianshavealreadydoneso ParAtt_JordanDoneEnough
	rename	TheSyriancrisishasincreased ParAtt_RentalPrice
	rename	TheSyriancrisishasmadeitmo	ParAtt_Jobs
	
	** Label Parental Attitudes
	label var ParAtt_Tragedy "Jordanians morally obliged to help"
	label var ParAtt_JordanDoneEnough "Jordan has done enough"
	label var ParAtt_RentalPrice "Rental prices increased"
	label var ParAtt_Jobs "Jobs more difficult to find"

* Create contact variables, family history, demograpics & SES proxies
	do code/createControlVariables	
	
	gen NumFriendsOutGroup_org = NumFriendsOutGroup
	
	** Label contact variables and family history
	label var OriginToAmman "Distance from origin to Amman (in 100km)"
	label var JordanianRelatives "Having Jordanian relatives (=1)"
	label var SyrianRelatives "Having Syrian relatives (=1)"
	label var TimeGoingToThisSchool "Time going to that school (yrs)"
	label var TimeInJordan "Time in Jordan (yrs)"
	label var TimeInJordan_long "Time in Jordan > 8 yrs (=1)"
	label var PalestinianRoots "Having Palestinian roots (=1)"
	label var NumFriendsOutGroup "Number of out-group friends"
	label var NumFriendsOutGroup_org "Number of out-group friends"
	label var FriendsFromOutGroupAtSchool "Out-group friends at school (=1)"
	
	** Label Demographics
	label var nChildren "Number of children in the household"
	label var parentMale "Surveyed parent is male (=1)"
	label var parentEducation "Parental eucation"
	label var governorateIncome "Avg. income in governorate"
	label var parentsHaveSmartphone "Smartphone ownership (=1)"
	label var parentsHaveBooks "Have books at home (=1)"	
	label var Whatisyournationality "Nationality"
	label var OtherWriteInRequiredWhat "Nationality - Other"
	
* Use class id as cluster variable
	gen clustering = ClassID
	label var clustering "Cluster variable: Class ID"

* Remove variables no longer used
	drop Shift	
	drop treatment 
	drop Game1_Keeping
	drop Game2_Keeping
	drop Game3_Giving_In

* Save clean dataset
save "1_input\Jordan_Dataset_Clean.dta", replace


****************************************************
* Teacher's Questionnaire
****************************************************			

	import delimited "0_data_raw_ascii\Jordan_TeachersQuestionnaire_Raw.csv", encoding(UTF-8) case(preserve) clear

	** Give Stata/Windows 3 Secs to process the data management
	sleep 3000

	** Generate Sample Indicator
	gen selectedSchool = 0
	replace selectedSchool = inlist(NumberofSchool, 3,7,8,9,15,17,18,22,24,26,30,35,46)

	** Encode information in order to perform numerical tests
	encode Doyouthinktha~w, generate(Num_Doyouthinkthatthesessionsw) // Do you think that the sessions were effective in addressing social cohesion?  	
	label define Num_Doyouthinkthatthesessionsw 0 "Don't know " 1 "Effective" -1 "Ineffective " 2 "Very effective" -2 "Very ineffective ", replace
	replace Num_Doyouthinkthatthesessionsw = Num_Doyouthinkthatthesessionsw - 1
	replace Num_Doyouthinkthatthesessionsw = -1 if Num_Doyouthinkthatthesessionsw == 2
	replace Num_Doyouthinkthatthesessionsw = 2 if Num_Doyouthinkthatthesessionsw == 3
	replace Num_Doyouthinkthatthesessionsw = -2 if Num_Doyouthinkthatthesessionsw == 4
	
	encode MoodDidyouobs~v, generate(Num_MoodDidyouobserveanypositiv) // Mood: Did you observe any positive changes in the students?
	encode BehaviorsDidy~o, generate(Num_BehaviorsDidyouobserveanypo) // Behaviors: Did you observe any positive changes in the students?
	encode Participation~y, generate(Num_ParticipationInClassroomDidy) // Participation In Classroom: Did you observe any positive changes in the students?
	encode ConfidenceDid~p, generate(Num_ConfidenceDidyouobserveanyp) // Confidence: Did you observe any positive changes in the students?
	encode CooperationDi~y, generate(Num_CooperationDidyouobserveany) // Cooperation: Did you observe any positive changes in the students?
	encode MotivationToR~r, generate(Num_MotivationToReadDidyouobser) // Motivation To Read: Did you observe any positive changes in the students?
	
	keep   HowmanyMEMTAsessionsdidyou   Num_MoodDidyouobserveanypositiv Num_BehaviorsDidyouobserveanypo Num_ParticipationInClassroomDidy Num_ConfidenceDidyouobserveanyp Num_CooperationDidyouobserveany Num_MotivationToReadDidyouobser Num_Doyouthinkthatthesessionsw selectedSchool
	
	local binary_list Num_MoodDidyouobserveanypositiv Num_BehaviorsDidyouobserveanypo Num_ParticipationInClassroomDidy Num_ConfidenceDidyouobserveanyp Num_CooperationDidyouobserveany Num_MotivationToReadDidyouobser
	foreach var of varlist `binary_list' {
		replace `var' = `var' - 1
	}

	** Label Evaluation variables
	label var HowmanyMEMTAsessionsdidyou "Number of conducted MEMTA sessions"
	label var Num_Doyouthinkthatthesessionsw "Effectiveness of the sessions in addressing social cohesion (scale from -2 (very ineffective) to 2 (very effective))?"
	label var Num_MoodDidyouobserveanypositiv "Mood: Positive changes observed (=1)"
	label var Num_BehaviorsDidyouobserveanypo "Behavior: Positive changes observed (=1)"
	label var Num_ParticipationInClassroomDidy "Participation In Classroom: Positive changes observed (=1)"
	label var Num_ConfidenceDidyouobserveanyp "Confidence: Positive changes observed (=1)"
	label var Num_CooperationDidyouobserveany "Cooperation: Positive changes observed (=1)"
	label var Num_MotivationToReadDidyouobser "Motivation To Read: Positive changes observed (=1)"
	label var selectedSchool "School was selected for the study (=1)"
	
	save "1_input\Jordan_TeachersQuestionnaire_Clean.dta", replace



****************************************************
* Demographics of entire UNICEF sample in comparison with study sample
****************************************************	
* Full Baseline and Endline
	import delimited "0_data_raw_ascii\Jordan_UNICEF_Sample_Demographics_Raw.csv", encoding(UTF-8) case(preserve) clear

	* Give Stata/Windows 3 Secs to process the data management
	sleep 3000
	
	* Clean some data
	destring NumberWhodoyoulivewith, replace
	rename NumberWhodoyoulivewith nHouseholdMembers
	
	encode Sex, generate(childMale)
	replace childMale = childMale - 1
	replace childMale = 0 if !missing(childMale) & childMale == 2
	label define childMale 0 "Female" 1 "Male", replace
	drop Sex
	
	encode Doyouhaveasmartphoneathome, generate(childHasSmartphone)
	replace childHasSmartphone = childHasSmartphone - 1 if !missing(childHasSmartphone)
	label define childHasSmartphone 0 "No" 1 "Yes", replace
	drop Doyouhaveasmartphoneathome
	
	rename Howlonghaveyoubeengoingto TimeGoingToThisSchool
	
	gen FriendsFromOutGroupAtSchool = 1 if Doyouhaveanyfriendsfromthe == "Yes"
	replace FriendsFromOutGroupAtSchool = 0 if missing(FriendsFromOutGroupAtSchool)
	drop Doyouhaveanyfriendsfromthe

	gen NumSyrianFriends = YesDoyouhaveanyfriendswho
	replace NumSyrianFriends = 0 if missing(NumSyrianFriends)
	gen NumJordanianFriends = YesDoyouhaveanyfriendswh2
	replace NumJordanianFriends = 0 if missing(NumJordanianFriends)
	gen NumFriendsOutGroup = (Shift=="M")*NumSyrianFriends + NumJordanianFriends*(Shift=="A")
	drop YesDoyouhaveanyfriendswho YesDoyouhaveanyfriendswh2 NumJordanianFriends NumSyrianFriends
	
	** Label contact and control variables
	label var TimeGoingToThisSchool "Time going to that school (yrs)"
	label var NumFriendsOutGroup "Number of out-group friends"
	label var FriendsFromOutGroupAtSchool "Out-group friends at school (=1)"
	label var childMale "Child is a boy (=1)"
	label var nHouseholdMembers "Household Size"
	label var childHasSmartphone "Smartphone ownership (=1)"
	label var WZBSample "School was selected for the study (=1)"
	label var Shift "Shift"
	
	save "1_input\Jordan_UNICEF_Sample_Demographics_Clean.dta", replace

* Have a nice day!
